package com.example.persistence;

import java.util.List;

import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TableLayout;
import android.widget.TableRow;

import com.example.R;
import com.example.entity.ViewStatus;
import com.example.entity.ViewTag;
import com.example.module.downloader.ExtendedImageDownloader;
import com.example.util.ViewUtil;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;

/**
 * @author Sergey Tarasevich (nostra13[at]gmail[dot]com)
 */
public abstract class ImageActivity extends SwitchActivity {
	protected abstract void doShare(List<View> selectedViews);
	protected abstract void doDelete(List<View> selectedViews);

	protected ImageLoader imageLoader = ImageLoader.getInstance();
	private boolean instanceStateSaved;
	public boolean isSelectable;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
			.threadPriority(Thread.NORM_PRIORITY - 2)
			.memoryCacheSize(2 * 1024 * 1024) // 2 Mb
			.denyCacheImageMultipleSizesInMemory()
			.discCacheFileNameGenerator(new Md5FileNameGenerator())
			.imageDownloader(new ExtendedImageDownloader(getApplicationContext()))
			.tasksProcessingOrder(QueueProcessingType.LIFO)
			.enableLogging() // Not necessary in common
			.build();
		
		// Initialize ImageLoader with configuration.
		ImageLoader.getInstance().init(config);
		super.onCreate(savedInstanceState);
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.menu_option, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
			case R.id.item_share:
				displayActionBar(false);
				displayShareBar(true);
//				imageLoader.clearMemoryCache();
				return true;
			case R.id.item_delete:
				displayShareBar(false);
				displayActionBar(true);
//				imageLoader.clearDiscCache();
				return true;
			default:
				return false;
		}
	}
	
	private TableRow displayMenuOption(boolean visiable, int resouceId) {
		isSelectable = visiable;
		
		TableLayout optionMenu = (TableLayout) findViewById(R.id.optionMenu);
		if (optionMenu == null) {
			optionMenu = (TableLayout) getLayoutInflater().inflate(R.layout.menu_option, null);
			FrameLayout.LayoutParams  layoutParams = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.BOTTOM|Gravity.RIGHT); 
			addContentView(optionMenu, layoutParams);
		}
		
		optionMenu.setVisibility(visiable ? View.VISIBLE : View.GONE);
		TableRow menu = (TableRow) optionMenu.findViewById(resouceId);
		menu.setVisibility(visiable ? View.VISIBLE : View.GONE);
		return menu;
	}
	
	private void displayActionBar(boolean visiable) {
		TableRow menu = displayMenuOption(visiable, R.id.actionMenu);
		ImageView cancel = (ImageView) menu.findViewById(R.id.cancel);
		ImageView delete = (ImageView) menu.findViewById(R.id.delete);
		
		cancel.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				displayActionBar(false);
			}
		});
		
		delete.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				ViewGroup container = (ViewGroup) findViewById(R.id.container);
				List<View> selectedViews = ViewUtil.findViewsByStatus(container, ViewStatus.SELECTED);
				doDelete(selectedViews);
				deselectViews(selectedViews);
				displayActionBar(false);
			}
		});
	}

	private void displayShareBar(boolean visiable) {
		TableRow menu = displayMenuOption(visiable, R.id.shareMenu);
		ImageView kakaoLink = (ImageView) menu.findViewById(R.id.kakaoLink);
//		ImageView facebookLink = (ImageView) menu.findViewById(R.id.facebookLink);
//		ImageView twitterLink = (ImageView) menu.findViewById(R.id.twitterLink);
		
		kakaoLink.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				ViewGroup container = (ViewGroup) findViewById(R.id.container);
				List<View> selectedViews = ViewUtil.findViewsByStatus(container, ViewStatus.SELECTED);
				doShare(selectedViews);
				deselectViews(selectedViews);
				displayActionBar(false);
			}
		});
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		instanceStateSaved = true;
	}

	@Override
	protected void onDestroy() {
		if (!instanceStateSaved) {
			imageLoader.stop();
		}
		super.onDestroy();
	}
	
	@Override
	public void onBackPressed() {
		View optionMenu = findViewById(R.id.optionMenu);
		if (optionMenu != null) {
			ViewGroup parentView = (ViewGroup) optionMenu.getParent();
			parentView.removeView(optionMenu);
			
			for (View view : ViewUtil.findViewsByStatus(parentView, ViewStatus.SELECTED)) {
				view.findViewById(R.id.selectedImage).setVisibility(View.GONE);
				view.setTag(null);
			}
			
			isSelectable = false;
		}
		else
			super.onBackPressed();
	}

	public void toggleView(int position, View view) {
		ViewTag viewTag = view.getTag() == null ?  null : (ViewTag) view.getTag();
		
		if (viewTag != null && viewTag.getViewStatus() == ViewStatus.SELECTED)
			deselectView(position, view);
		else
			selectView(position, view);		
	}
	
	public void selectView(View view) {
		ViewTag viewTag = new ViewTag(ViewStatus.SELECTED);
		view.findViewById(R.id.selectedImage).setVisibility(View.VISIBLE);
		view.setTag(viewTag);
	}
	
	public void selectView(int position, View view) {
		ViewTag viewTag = new ViewTag(position ,ViewStatus.SELECTED);
		view.findViewById(R.id.selectedImage).setVisibility(View.VISIBLE);
		view.setTag(viewTag);
	}
	
	public void deselectView(View view) {
		ViewTag viewTag = new ViewTag(ViewStatus.UNSELECTED);
		view.findViewById(R.id.selectedImage).setVisibility(View.GONE);
		view.setTag(viewTag);
	}
	
	public void deselectView(int position, View view) {
		ViewTag viewTag = new ViewTag(position, ViewStatus.UNSELECTED);
		view.findViewById(R.id.selectedImage).setVisibility(View.GONE);
		view.setTag(viewTag);
	}
	
	public void deselectViews(List<View> selectedViews) {
		for (View view : selectedViews) {
			deselectView(view);
		}
		
	}
}
